interface ItemHandle {
    public int run(Item obj, Object Args);
}

class Item {
    private int id;
    private String title;
    private ItemHandle handle = null;

    public Item() {}

    public Item(int id) {
        this.id = id;
    }
    public Item(int id, String title) {
        this.id = id;
        this.title = title;
    }

    public Item(int id, String title, ItemHandle handle) {
        this.id = id;
        this.title = title;
        this.handle = handle;
    }

    public Item set(int id) {
        this.id = id;
        return this;
    }

    public Item set(String title) {
        this.title = title;
        return this;
    }

    public Item set(ItemHandle handle) {
        this.handle = handle;
        return this;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public ItemHandle getRun() {
        return handle;
    }
}

public class Menu {
    MList llist = new MList();
    static private int ids = 0;
    static private ItemHandle common = new ItemHandle() {
        public int run(Item obj, Object Args) {
            System.out.printf("正在运行: %s 击中!\n", obj.getTitle());
            return 0;
        }
    };

    public Menu() {}

    public Menu(String ... titles) {
        for (int i = 0; i < titles.length; ++i) {
            add(titles[i]);
        }
    }

    public Menu add(int id, String title, ItemHandle handle) {
        if (id == 0 || id > ids)
            ids = id;
        llist.append(new Item(ids, title, handle));
        return this;
    }

    public Menu add(String title, ItemHandle handle) {
        llist.append(new Item(++ids, title, handle));
        return this;
    }

    public Menu add(String title) {
        llist.append(new Item(++ids, title, null));
        return this;
    }

    public Menu set(ItemHandle common) {
        this.common = common;
        return this;
    }

    public Menu run() {
        Item item = null;
        int num;
        while (true) {
            while ((item = (Item)llist.getNext()) != null) {
                System.out.printf("%d. %s\n", item.getId(), item.getTitle());
            }
            System.out.printf("%d. 退出!\n", ids + 1);
            num = Input.getInt("请输入: ", 1, ids + 1);
            if (num == ids + 1)
                break;
            item = (Item)llist.find(num);
            ItemHandle handle = item.getRun();
            if (handle == null) {
                num = common.run(item, null);
            } else {
                num = handle.run(item, null);
            }

            if (num != 0)
                break;
        }
        return this;
    }

    static public void main(String [] args) {
        Menu menu = new Menu("1111", "2222", "3333");

        menu.set(new ItemHandle() {
            public int run(Item obj, Object Args) {
                System.out.printf("自己实现正在运行: %s 击中!\n", obj.getTitle());
                return 0;
            }
        });

        menu.add("aaaa");
        menu.add("bbbb");
        menu.add("cccc", new ItemHandle() {
            public int run(Item obj, Object Args)  {
                System.out.printf("----> %s 自定义击中\n", obj.getTitle());
                return 1;
            }
        });

        menu.run();
    }
}
